//
//  HMNetworkTools.swift
//  02-AFN网络封装
//
//  Created by apple on 16/8/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking
// 网络请求枚举
enum HMNetworkToolsMethod: String {
    // GET 请求
    case GET = "GET"
    // POST 请求
    case POST = "POST"
}
/*
    - 对afn进行封装
    - 提供一个可以发送get和post请求的公共方法
    - 帮助当前app 请求所有接口数据
*/
class HMNetworkTools: AFHTTPSessionManager {
    // 全局访问点
    static let sharedTools:HMNetworkTools = {
        let tools = HMNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    /**
     网络请求公共方法
     
     - parameter method:     请求方式
     - parameter urlString:  请求urlString
     - parameter parameters: 请求参数
     - parameter success:    请求成功的闭包
     - parameter failure:    请求失败的闭包
     */
    func request(method: HMNetworkToolsMethod, urlString: String, parameters: AnyObject?, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // get请求
        if method == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                // 03
                success(response: response)
                }, failure: { (_, error) -> Void in
                    // 03
                  failure(error: error)
            })
            
        }else {
            // post 请求
            POST(urlString, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                // 03
                success(response: response)
                }, failure: { (_, error) -> Void in
                    // 03
                    failure(error: error)
            })
            
        }
        
    }
 
}
//MARK : -请求微博页面数据
extension HMNetworkTools{
    
    func homeLoadData(success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        //请求UrlString
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        //请求参数
        let parmas = [
            "access_token":LGUserAccountViewModel.sharedTools.userAccount!.access_token!
        ]
        request(HMNetworkToolsMethod.GET, urlString: urlString, parameters: parmas, success: success, failure: failure)

    }
    
    
    
    
}
//MARK : -请求授权
extension HMNetworkTools{
    func oauthLoadUserAccount(code : String , success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        //请求urlstring
        let urlString = "https://api.weibo.com/oauth2/access_token"
        //请求参数
        let params = [
            "client_id" : WBAppKey,
            "client_secret" : WBAppSecret,
            " grant_type" : "authorization_code",
            "code" : code,
            "redirect_uri" : WBRedirect_uri
            
        ]
        request(.POST, urlString: urlString, parameters: params, success: success, failure: failure)

    }
    
    //请求个人信息
    func oauthloadUserInfo(userAccount : LGUserAccountModel , success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        //请求Urlstring
        let urlString = "https://api.weibo.com/2/users/show.json"
        //请求参数
        let param = [
            "access_token" : userAccount.access_token!,
            "uid" : userAccount.uid!
        ]
        request(HMNetworkToolsMethod.GET, urlString: urlString, parameters: param, success: success, failure: failure)
    }
   
}

//MARK: - 请求天气接口
extension HMNetworkTools {
    func getWeatherData(success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        let urlString = "http://www.weather.com.cn/data/sk/101010100.html"
        request(HMNetworkToolsMethod.GET, urlString: urlString, parameters: nil, success: success, failure: failure)
    }
}
